package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.pojo.backend.Employee;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;




    /**
     * 登入
     * @param
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee) {
        return employeeService.login(employee);
    }

    /**
     * 登出
     *
     * @param
     */

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        return employeeService.logout(request);
    }

    /**
     * 添加
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R add(HttpServletRequest request, @RequestBody Employee employee) {
        return R.success(employeeService.add(request, employee));
    }


    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R select(Integer page,Integer pageSize,String name){
        return employeeService.select(page,pageSize,name);
    }


    /**
     * 按id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectByid(@PathVariable  Integer id){
        Employee employee = employeeService.selectByid(id);
        if (employee!=null){
            return R.success(employee);
        }else {
            return R.error("查无此人");
        }
    }

    /**
     * 修改
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R updatemp(HttpServletRequest request, @RequestBody Employee employee) {
        return employeeService.updatemp(request, employee);
    }

}


